$(function () {
	
	$.get("../station/area/list", function(r){
		vm.area = r.listArea;
        var list = r.listArea;
        var html='';
        html+='<option value="0">请选择区域</option>';
        $.each(list,function(index,item){
        	
        	html+='<option value="'+item.id+'">'+item.name+'</option>';
        	
        });
        $("#liebiao").html(html);
        
    });
	
    $("#jqGrid").jqGrid({
        url: '../station/list',
        datatype: "json",
        colModel: [			
			{ label: '编号', name: 'code', width: 80 }, 			
			{ label: '所属区域', name: 'idArea', width: 80 ,formatter:customFmatter}, 			
			{ label: '基站名称', name: 'name', width: 80 }, 			
			{ label: '坐标X', name: 'postionX', width: 80 }, 			
			{ label: '坐标Y', name: 'postionY', width: 80 }			
        ],
		viewrecords: true,
        height: 'auto',
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
	
});

function customFmatter(cellvalue, options, rowObject){ 
	
	var name;
	
	$.each(vm.area,function(index,item){
		
		if(cellvalue==item.id){
			
			name=item.name;
		}
		
	});

    return name;  
}

function show_sub(v){   

	vm.showList = true;
	var page = $("#jqGrid").jqGrid('getGridParam','page');
	$("#jqGrid").jqGrid('setGridParam',{
		  postData:{'idArea': v},
        page:page
    }).trigger("reloadGrid");
} 
var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		station: {},
		area:{}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.station = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.station.id == null ? "../station/save" : "../station/update";
			$.ajax({
				type: "POST",
			    url: url,
			    data: JSON.stringify(vm.station),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: "../station/delete",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get("../station/info/"+id, function(r){
                vm.station = r.station;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		}
	}
});